package com.asiainfo.service;

import com.asiainfo.dao.OperatorData;
import com.asiainfo.domain.Card;
import com.asiainfo.domain.Member;

public class AddMemberInfo {
	/**
	 * 增加会员成员
	 * 
	 * @param memberName
	 *            会员名
	 * @param memberAddr
	 *            会员地址
	 * @param memberWork
	 *            会员工作
	 * @return MemberId
	 */
	public static int addMember(String memberName, String memberAddr,
			String memberWork) {
		//根据表明和字段获取到当前表中最大的ID的值，并+1做为新的member对象的id属性
		String tableName = "member";
		String colName = "member_id";
		int memberId = OperatorData.getMaxId(tableName, colName) + 1;
		Member member = new Member();
		member.setMemberId(memberId + 1);
		member.setMemberName(memberName);
		member.setMemberAddr(memberAddr);
		member.setMemberWork(memberWork);
		//调用增加Member方法，增加新构建的Member对象
		if (!OperatorData.addMember(member)) {
			memberId = 0;
			System.out.println("创建客户失败");
		}
		System.out.println("创建客户成功");
		return memberId;
	}

	/**
	 * 根据会员名字创建会员卡
	 * @param memberName
	 *            会员名字
	 * @param cardType
	 *            会员卡类型
	 * @param cardTimes
	 *            会员卡次数或年数
	 * @return cardID
	 */
	public int addCard(String memberName, String cardType, int cardTimes) {
		
		String tableName = "card";
		String colName = "card_id";
		int cardId = OperatorData.getMaxId(tableName, colName) + 1;
		String cardState = "U";
		int memberId = OperatorData.getMemIdByName(memberName);
		Card card = new Card();
		card.setCardId(cardId);
		card.setMemberId(memberId);
		card.setCardState(cardState);
		card.setCardTimes(cardTimes);
		card.setCardType(cardType);
		if (!OperatorData.addCard(card)){
			cardId = 0;
		}
		return cardId;
	}
	/**
	 * 根据会员Id创建会员卡
	 * @param memberId 会员ID
	 * @param cardType 会员卡类型
	 * @param cardTimes 会员卡次数或年数
	 * @return cardId
	 *
	 * 该函数为上面函数的重载函数,用于通过memberId来增加会员卡,暂时未使用
	 */
	public static int addCard(int memberId, String cardType, int cardTimes){
		String tableName = "card";
		String colName = "card_id";
		int cardId = OperatorData.getMaxId(tableName, colName) + 1;
		String cardState = "U";
		Card card = new Card();
		card.setCardId(cardId);
		card.setMemberId(memberId);
		card.setCardState(cardState);
		card.setCardTimes(cardTimes);
		card.setCardType(cardType);
		if (!OperatorData.addCard(card)){
			cardId = 0;
		}
		return cardId;
	}
}
